package com.esp.soccertutor.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;


import com.esp.soccertutor.R;
import com.esp.soccertutor.android.fbreader.FBReader;
import com.esp.soccertutor.uc.GS_Home_Bottom_Menu;
import com.esp.soccertutor.util.Const;
import com.esp.soccertutor.util.Log;
import com.esp.soccertutor.util.Pref;

public class GSHome extends Activity{

	private GS_Home_Bottom_Menu mMenu;
	private CheckBox _cboxautoload;
	private Button btnSettingRead;
	private TextView txtSetting;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        requestWindowFeature(Window.FEATURE_NO_TITLE);// hide window title
        
        setContentView(R.layout.gs_home);//Set Layout
        
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
        this.mMenu = new GS_Home_Bottom_Menu(this, getLayoutInflater());
        
        txtSetting = (TextView)findViewById(R.id.txtSetting);
        txtSetting.setTypeface(Const.tf);
        
        _cboxautoload=(CheckBox)findViewById(R.id._cboxAutoLoadBk);
        checkCBoxValue();
        _cboxautoload.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
						Log.print("ACT_GSHOME::OnCreate:CheckBox ",""+_cboxautoload.isChecked());
						Pref.setValue("GEN_SET_AUTO_LOAD_LAST_BOOK", "YES");
						
				}else{
						Log.print("ACT_GSHOME::OnCreate:CheckBox ",""+_cboxautoload.isChecked());
						Pref.setValue("GEN_SET_AUTO_LOAD_LAST_BOOK", "NO");
				}
			}
		});
        btnSettingRead=(Button)findViewById(R.id.btnSettingRead);
        btnSettingRead.setOnClickListener(new OnClickListener() {
			
			
			public void onClick(View v) {
				startActivity(new Intent(GSHome.this,FBReader.class));
			}
		});
    }
    
    private void checkCBoxValue(){
    	
    	if(Pref.getValue("GEN_SET_AUTO_LOAD_LAST_BOOK","YES").toString().endsWith("YES")){
    		_cboxautoload.setChecked(true);
    	}
    	
    	
    }
    @Override
	public void onResume(){   	
    	super.onResume();
    }	
    
    @Override
	public boolean onKeyDown(int keyCode, KeyEvent event) { 
	    if (keyCode == KeyEvent.KEYCODE_MENU) {
	    	doMenu();
	    	return true; //always eat it!	    	
	    }
	    else if(keyCode == KeyEvent.KEYCODE_BACK)
	    {
	    	if (mMenu.isShowing()) {
				mMenu.hide();
				return true;
	    	}
	    }
		return super.onKeyDown(keyCode, event); 
	} 
	
	private void doMenu() {
		if (mMenu.isShowing()) {
			mMenu.hide();
		} else {
			//Note it doesn't matter what widget you send the menu as long as it gets view.
			mMenu.show(findViewById(R.id._rl_bodySetting));
		}
	}
}
